package com.wsoft.system.controller;

import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import com.wsoft.system.entity.SysMemberEntity;
import com.wsoft.system.query.member.MemberGetQuery;
import com.wsoft.system.query.member.MemberInsertQuery;
import com.wsoft.system.query.member.MemberListQuery;
import com.wsoft.system.query.member.MemberUpdateQuery;
import com.wsoft.system.service.ISysMemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author juiet
 * @since 2024年09月03日 10:35:18
 */
@RestController
@Tag(name = "用户管理")
@RequestMapping("/sys/member")
public class SysMemberController {

    @Resource
    private ISysMemberService memberService;

    @Operation(summary = "新增用户",description = "新增用户")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid MemberInsertQuery query) {
        boolean b = memberService.register(query);
        if (b){
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "修改用户",description = "修改用户")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid MemberUpdateQuery query) {
        boolean b = memberService.update(query);
        if (b){
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "删除用户",description = "删除用户")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Long id) {
        boolean b = memberService.delete(id);
        if (b){
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }
    @Operation(summary = "查看用户详情",description = "查看用户详情")
    @ApiResponse(responseCode = "200", description = "OK")
    @GetMapping("/get")
    public ResponseResult getMember(@RequestParam("id") Long id) {
        return ResponseResult.success(memberService.getMember(id));
    }

    @Operation(summary = "用户列表",description = "分页")
    @PostMapping("/list")
    public ResponseResult pageList(@RequestBody @Valid MemberListQuery query) {
        return ResponseResult.success(ResponsePage.restPage(memberService.pageList(query)));
    }

    @Operation(summary = "批量获取用户信息",description = "批量获取用户信息")
    @PostMapping("/getByIds")
    public ResponseResult<List<SysMemberEntity>> getByIds(@RequestBody @Valid MemberGetQuery query) {
        List<SysMemberEntity> list = memberService.listByIds(query.getIds());
        return ResponseResult.success(list);
    }
}
